package com.yeraphael.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yeraphael.annotation.Log;
import com.yeraphael.entity.Clazz;
import com.yeraphael.entity.ClazzQueryParam;
import com.yeraphael.entity.PageBeandf;
import com.yeraphael.mapper.ClazzMapper;
import com.yeraphael.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    public static String compareData(Clazz clazz) {
        LocalDate nowDate = LocalDate.now();
        String result = null;

        if( nowDate.isBefore(clazz.getBeginDate()) ) {
            result = "未开班";
        } else if (nowDate.isAfter(clazz.getEndDate())) {
            result = "已结课";
        } else {
            result = "已开班";
        }
        return result;
    }

    @Override
    public PageBeandf getAll(ClazzQueryParam param) {
//        1.设置分页
        PageHelper.startPage(param.getPage(), param.getPageSize());

//        3.调用Mapper层方法
        List<Clazz> clazzList = clazzMapper.getAll(param);
        if( !clazzList.isEmpty() ){
            clazzList.forEach(clazz -> {
                clazz.setStatus(compareData(clazz));
            });
        }

        Page page = (Page) clazzList;

        return new PageBeandf(page.getTotal(), clazzList);
    }

    @Log
    @Override
    public void addClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.addClazz(clazz);
    }

    @Override
    public Clazz getById(Integer id) {
        Clazz clazz = clazzMapper.getById(id);
        return clazz;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateClazz(clazz);
    }


    @Override
    public void deleteClazz(Integer id) {
        clazzMapper.deleteClazz(id);
    }

    @Override
    public List<Clazz> getAllList() {
        List<Clazz> clazzList = clazzMapper.getAllList();
        return clazzList;
    }

}
